//
//  String+YMTool.swift
//  YMToolKit
//
//  Created by YDWY on 2017/4/24.
//  Copyright © 2017年 YDWY. All rights reserved.
//

import Foundation

extension  String {
    
    
    //MARK: - 判断是否是手机号
    static func  ym_checkPhoneNumber(str:String) -> Bool {
        if str.isEmpty {
            return false
        }
        let  pattern = "1[3578]\\d{9}"
        return ym_checkfunc(pattern: pattern, str: str)
      
    }
    
    //MARK: - 判断是否是邮箱
    static func ym_checkEmailNumber(str:String) -> Bool {
        if str.isEmpty {
            return false
        }
        let  pattern = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
        return ym_checkfunc(pattern: pattern, str: str)
    }
    
    
    
    //MARK: - 判断省份证号
    static func ym_checkIdentityCard(str:String) -> Bool {
        if str.isEmpty {
            return false
        }
        let  pattern = "^(\\d{14}|\\d{17})(\\d|[xX])$"
        return ym_checkfunc(pattern: pattern, str: str)
    }
    
    
    
    //MARK: - 判断昵称
    static func ym_checkNickName(str:String) -> Bool {
        if str.isEmpty {
            return false
        }
        let  pattern = "^[\\u4e00-\\u9fa5]{4,8}$"
        return ym_checkfunc(pattern: pattern, str: str)
    }
    
    //MARK: - 判断密码
    static func ym_checkPassword(str:String) -> Bool {
        if str.isEmpty {
            return false
        }
        let  pattern =  "^[a-zA-Z0-9]{6,20}+$"
        return ym_checkfunc(pattern: pattern, str: str)
    }
    
    
    //MARK: - 用户名
    static func ym_checkUserName(str:String) -> Bool {
        if str.isEmpty {
            return false
        }
        let  pattern =  "^[A-Za-z0-9]{6,20}+$"
        return ym_checkfunc(pattern: pattern, str: str)
    }
    

    
    //MARK: - 助手方法
    static func ym_checkfunc(pattern:String, str:String)->Bool{
    
        guard let regex  = try?  NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options(rawValue:0)) else { return  false }
        
        let res = regex.matches(in: str, options: NSRegularExpression.MatchingOptions(rawValue:0), range: NSMakeRange(0, str.characters.count))
        if res.count > 0 {
            return true
        }
        return false
    }
    
    
    
    
    
}
